<?php

namespace Modules\Agent\Entities;

use Modules\User\Entities\User;
use ThrowException;
use Setting; 
use DB;

class Agent extends User
{
    protected $guarded = [];
    protected $table = 'users';
    static public $resourceName = '';

    // 此等级下所有分销员
    public function agent_level()
    {
        return $this->belongsTo('\Modules\Agent\Entities\AgentLevel', 'agent_level_index',  'level');
    }

    public function scopeUserPhone($query, $phone)
    {
        return $query->where('phone', $phone);
    }

    public function scopeAgentLevel($query, $level)
    {
        return $query->where('agent_level_index', $level);
    }

    public function scopeType($query, $type) 
    {
        if ($type === 'blocked') {
            return $query->blocked();
        }
        elseif ($type === 'marked') {
            return $query->whereNotNull('marked_at');
        }
        elseif ($type === 'level1') {
            return $query;
        }
        else {
            // 按平台查看
            return $query->platformType($type);
        }
    }

    // 是否是团队长
    public  function isAgentLeader()
    {
        return $this->agent_level_index === 4;
    }
    
    static public function beforeGetList($options)
    {
        if (isset($options['agent_level'])) {
            $options['scopes']['agentLevel'] = $options['agent_level'];
            // $options['where']['status'] = $options['status'];
        }

        return parent::beforeGetList($options);
    }
}
